/*******************************************************************************
   @file    dev_IuEab.h
   @author  H.M.Shim (elecage@gmail.com)
   @version 
   @date    2013. 7. 31.
   @brief   
  ******************************************************************************
  The MIT License (MIT)

Copyright (c) 2013 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  ******************************************************************************
  */



/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef DEV_IUEAB_H_
#define DEV_IUEAB_H_

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "devio.h"
#include "stm32f4xx_gpio.h"
#include "dev_gpio.h"
#include "dev_i2c.h"
#include "dev_spi.h"
#include "ads119x.h"
#include "isl22343.h"

#define NUM_EAB			3
#define NUM_ADC_CHANNEL	4
/* Exported typedef -----------------------------------------------------------*/
typedef struct _Dev_IuEab_flag{
	int8_t 			stat;				/**< @brief 장치의 상태 플래그 */
	uint16_t 		address;			/**< @brief 읽고 쓰기를 할 주소 */
	int32_t			timeout;			/**< @brief 타임아웃 */
	int32_t			i2c_dd;
	int32_t			spi_dd;
	int32_t			gpio_dd;
	uint8_t			adc_Reg_id;
	uint8_t			adc_Reg_config1;
	uint8_t			adc_Reg_config2;
	uint8_t			adc_Reg_config3;
	uint8_t			adc_Reg_config4;
	uint8_t			adc_Reg_channel[NUM_ADC_CHANNEL];
}__attribute__((packed))Dev_IuEab_flag;

enum Dev_IU_EAB_IOCTL_Param {
	DEV_IU_EAB_START,
	DEV_IU_EAB_STOP,
	DEV_IU_EAB_ADC_POWER_DOWN,
	DEV_IU_EAB_ADC_POWER_EN,
	DEV_IU_EAB_POWER_DOWN,
	DEV_IU_EAB_POWER_EN,
	DEV_IU_EAB_RESET,
	DEV_IU_EAB_SET_CONFIG1,
	DEV_IU_EAB_SET_CONFIG2,
	DEV_IU_EAB_SET_CONFIG3,
	DEV_IU_EAB_SET_CONFIG4,
	DEV_IU_EAB_SET_CHANNEL,
	DEV_IU_EAB_GET_CONFIG1,
	DEV_IU_EAB_GET_CONFIG2,
	DEV_IU_EAB_GET_CONFIG3,
	DEV_IU_EAB_GET_CONFIG4,
	DEV_IU_EAB_GET_CHANNEL,
	DEV_IU_EAB_SET_DAISY_EN,
	DEV_IU_EAB_SET_DAISY_DIS,
	DEV_IU_EAB_SET_INT_OSC,
	DEV_IU_EAB_SET_EXT_OSC,
	DEV_IU_EAB_SET_SR,
	DEV_IU_EAB_SET_CH_PD,
	DEV_IU_EAB_SET_CH_GAIN,
	DEV_IU_EAB_SET_CH_MUX,
	DEV_IU_EAB_GET_CH_PD,
	DEV_IU_EAB_GET_CH_GAIN,
	DEV_IU_EAB_GET_CH_MUX,
	DEV_IU_EAB_GET_ID,
};

typedef void (*AdcCallback)(int32_t* data);

#define DEV_IU_EAB_START_CMD		"start"
#define DEV_IU_EAB_STOP_CMD			"stop"
#define DEV_IU_EAB_ID_CMD			"id"
#define DEV_IU_EAB_ADC_POWER_DOWN_CMD 	"adc_down"
#define DEV_IU_EAB_ADC_POWER_EN_CMD 	"adc_en"
#define DEV_IU_EAB_POWER_DOWN_CMD		"power_down"
#define DEV_IU_EAB_POWER_EN_CMD			"power_en"
#define DEV_IU_EAB_RESET_CMD			"reset"
#define DEV_IU_EAB_SET_GAIN_CMD			"set_gain"
#define DEV_IU_EAB_SET_SR_CMD			"set_sr"
/* Exported constants --------------------------------------------------------*/
/** INA2331 감도 조절 포텐셔미터의 주소와 MAP */
#define DEV_IU_EAB_PRE_POT_CH1_ADDR		(ISL_ADDRESS | 0x00)
#define DEV_IU_EAB_PRE_POT_CH1_MAP		(ISL_REG_MAP_IVR0)

#define DEV_IU_EAB_PRE_POT_CH2_ADDR		(ISL_ADDRESS | 0x01)
#define DEV_IU_EAB_PRE_POT_CH2_MAP		(ISL_REG_MAP_IVR0)

#define DEV_IU_EAB_PRE_POT_CH3_ADDR		(ISL_ADDRESS | 0x02)
#define DEV_IU_EAB_PRE_POT_CH3_MAP		(ISL_REG_MAP_IVR0)

#define DEV_IU_EAB_PRE_POT_CH4_ADDR		(ISL_ADDRESS | 0x03)
#define DEV_IU_EAB_PRE_POT_CH4_MAP		(ISL_REG_MAP_IVR0)

/** INA2331 캘리브레이션 조절 포텐셔미터의 주소와 MAP */
#define DEV_IU_EAB_CAL_POT_CH1_ADDR		(ISL_ADDRESS | 0x05)
#define DEV_IU_EAB_CAL_POT_CH1_MAP		(ISL_REG_MAP_IVR0)

#define DEV_IU_EAB_CAL_POT_CH2_ADDR		(ISL_ADDRESS | 0x05)
#define DEV_IU_EAB_CAL_POT_CH2_MAP		(ISL_REG_MAP_IVR1)

#define DEV_IU_EAB_CAL_POT_CH3_ADDR		(ISL_ADDRESS | 0x04)
#define DEV_IU_EAB_CAL_POT_CH3_MAP		(ISL_REG_MAP_IVR2)

#define DEV_IU_EAB_CAL_POT_CH4_ADDR		(ISL_ADDRESS | 0x04)
#define DEV_IU_EAB_CAL_POT_CH4_MAP		(ISL_REG_MAP_IVR3)

/** OPA4330에서 HPF의 cutoff를 설정하는 포텐셔미터의 주소와 MAP */
#define DEV_IU_EAB_HPF_POT_CH1A_ADDR	(ISL_ADDRESS | 0x00)
#define DEV_IU_EAB_HPF_POT_CH1A_MAP		(ISL_REG_MAP_IVR1)
#define DEV_IU_EAB_HPF_POT_CH1B_ADDR	(ISL_ADDRESS | 0x00)
#define DEV_IU_EAB_HPF_POT_CH1B_MAP		(ISL_REG_MAP_IVR2)

#define DEV_IU_EAB_HPF_POT_CH2A_ADDR	(ISL_ADDRESS | 0x01)
#define DEV_IU_EAB_HPF_POT_CH2A_MAP		(ISL_REG_MAP_IVR1)
#define DEV_IU_EAB_HPF_POT_CH2B_ADDR	(ISL_ADDRESS | 0x01)
#define DEV_IU_EAB_HPF_POT_CH2B_MAP		(ISL_REG_MAP_IVR2)

#define DEV_IU_EAB_HPF_POT_CH3A_ADDR	(ISL_ADDRESS | 0x02)
#define DEV_IU_EAB_HPF_POT_CH3A_MAP		(ISL_REG_MAP_IVR1)
#define DEV_IU_EAB_HPF_POT_CH3B_ADDR	(ISL_ADDRESS | 0x02)
#define DEV_IU_EAB_HPF_POT_CH3B_MAP		(ISL_REG_MAP_IVR2)

#define DEV_IU_EAB_HPF_POT_CH4A_ADDR	(ISL_ADDRESS | 0x03)
#define DEV_IU_EAB_HPF_POT_CH4A_MAP		(ISL_REG_MAP_IVR1)
#define DEV_IU_EAB_HPF_POT_CH4B_ADDR	(ISL_ADDRESS | 0x03)
#define DEV_IU_EAB_HPF_POT_CH4B_MAP		(ISL_REG_MAP_IVR2)

#ifdef USE_KPU_BOARD

#define DEV_IU_EAB1_SPI_PORT			SPI1
#define DEV_IU_EAB1_SPI_NAME			((int8_t*)"spi1")
#define DEV_IU_EAB1_I2C_NAME			((int8_t*)"i2c1")
#define DEV_IU_EAB1_GPIO_NAME			((int8_t*)"gpioe")

#define DEV_IU_EAB2_SPI_PORT			SPI2
#define DEV_IU_EAB2_SPI_NAME			((int8_t*)"spi2")
#define DEV_IU_EAB2_I2C_NAME			((int8_t*)"i2c2")
#define DEV_IU_EAB2_GPIO_NAME			((int8_t*)"gpioe")

#define DEV_IU_EAB3_SPI_PORT			SPI3
#define DEV_IU_EAB3_SPI_NAME			((int8_t*)"spi3")
#define DEV_IU_EAB3_I2C_NAME			((int8_t*)"i2c3")
#define DEV_IU_EAB3_GPIO_NAME			((int8_t*)"gpioe")

#define DEV_IU_EAB_SPI_FLAG_POL			DEV_SPI_FLAG_POL_HIGH
#define DEV_IU_EAB_SPI_FLAG_PHA			DEV_SPI_FLAG_PHASE_1
#define DEV_IU_EAB_SPI_FLAG_FIRST_BIT	DEV_SPI_FLAG_MSB

#define DEV_IU_EAB_GPIO_PORT			GPIOE
#define DEV_IU_EAB1_GPIO_POWER_EN_PIN	GPIO_Pin_5
#define DEV_IU_EAB1_GPIO_ADC_PWDN_PIN	GPIO_Pin_14
#define DEV_IU_EAB1_GPIO_ADC_RESET_PIN	GPIO_Pin_5			/// 없음
#define DEV_IU_EAB1_GPIO_ADC_START_PIN	GPIO_Pin_1			///
#define DEV_IU_EAB1_GPIO_ADC_DRDY_PIN	GPIO_Pin_0			///
#define DEV_IU_EAB1_GPIO_ADC_PIN_SOURCE GPIO_PinSource0


#else
#define DEV_IU_EAB1_SPI_PORT			SPI2
#define DEV_IU_EAB1_SPI_NAME			((int8_t*)"spi2")
#define DEV_IU_EAB1_I2C_NAME			((int8_t*)"i2c1")
#define DEV_IU_EAB1_GPIO_NAME			((int8_t*)"gpioe")

#define DEV_IU_EAB2_SPI_PORT			SPI2
#define DEV_IU_EAB2_SPI_NAME			((int8_t*)"spi1")
#define DEV_IU_EAB2_I2C_NAME			((int8_t*)"i2c2")
#define DEV_IU_EAB2_GPIO_NAME			((int8_t*)"gpioe")

#define DEV_IU_EAB3_SPI_PORT			SPI2
#define DEV_IU_EAB3_SPI_NAME			((int8_t*)"spi3")
#define DEV_IU_EAB3_I2C_NAME			((int8_t*)"i2c3")
#define DEV_IU_EAB3_GPIO_NAME			((int8_t*)"gpioe")

#define DEV_IU_EAB_SPI_FLAG_POL			DEV_SPI_FLAG_POL_HIGH
#define DEV_IU_EAB_SPI_FLAG_PHA			DEV_SPI_FLAG_PHASE_1
#define DEV_IU_EAB_SPI_FLAG_FIRST_BIT	DEV_SPI_FLAG_MSB

#define DEV_IU_EAB_GPIO_PORT			GPIOE
#define DEV_IU_EAB1_GPIO_POWER_EN_PIN	GPIO_Pin_5
#define DEV_IU_EAB1_GPIO_ADC_PWDN_PIN	GPIO_Pin_6
#define DEV_IU_EAB1_GPIO_ADC_RESET_PIN	GPIO_Pin_4
#define DEV_IU_EAB1_GPIO_ADC_START_PIN	GPIO_Pin_3
#define DEV_IU_EAB1_GPIO_ADC_DRDY_PIN	GPIO_Pin_2
#define DEV_IU_EAB1_GPIO_ADC_PIN_SOURCE GPIO_PinSource2
#endif
/* Exported macro ------------------------------------------------------------*/

/* Exported variables ------------------------------------------------------- */

/* Exported functions ------------------------------------------------------- */
int32_t registerDev_IuEab(void);
int32_t dev_IuEabInit(int32_t dd);
int32_t dev_IuEabOpen(const int8_t* devname, int32_t flags);
ssize_t dev_IuEabWrite(int32_t dd, const void *buf, size_t n);
ssize_t	dev_IuEabRead(int32_t dd, void *buf, size_t nbytes);
ssize_t dev_IuEabIoctl(int32_t dd, int32_t request, void* data);
int32_t dev_IuEabClose(int32_t dd);

void dev_IuEabConfigEXTI(uint8_t portsource, EXTI_Config* config);

#ifdef USE_CMDLINE
int32_t dev_IuEabInitCmd(int32_t argc, int8_t *argv[]);
int32_t dev_IuEabOpenCmd(int32_t argc, int8_t *argv[]);
int32_t dev_IuEabReadCmd(int32_t argc, int8_t *argv[]);
int32_t dev_IuEabWriteCmd(int32_t argc, int8_t *argv[]);
int32_t dev_IuEabIoctlCmd(int32_t argc, int8_t *argv[]);
int32_t dev_IuEabCloseCmd(int32_t argc, int8_t *argv[]);
#endif /* USE_CMDLIN */
#endif /* DEV_IUEAB_H_ */
